<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('studentPatent.fields.patentName')" prop="patentName">
        <el-input
          v-model="queryParams.patentName"
          :placeholder="t('common.inputTextPlaceholder') + t('studentPatent.fields.patentName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('studentPatent.fields.reviewStatus')" prop="reviewStatus">
        <el-select
          v-model="queryParams.reviewStatus"
          :placeholder="t('common.selectTextPlaceholder') + t('studentPatent.fields.reviewStatus')"
          clearable
          class="!w-240px"
        >
          <el-option :label="t('studentPatent.audit.status.draft')" value="草稿" />
          <el-option :label="t('studentPatent.audit.status.waiting')" value="待审核" />
          <el-option :label="t('studentPatent.audit.status.passed')" value="已通过" />
          <el-option :label="t('studentPatent.audit.status.rejected')" value="已拒绝" />
          <el-option :label="t('studentPatent.audit.status.revoke')" value="已撤回" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <el-button type="primary" plain @click="openForm()">
          <Icon icon="ep:plus" class="mr-5px" />
          {{ t('action.add') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iStudentPatent-table1"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('studentPatent.fields.studentCode')"
        field="studentCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.studentName')"
        field="studentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.mentorCode')"
        field="mentorCode"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.mentorName')"
        field="mentorName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.patentName')"
        field="patentName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.patentStatus')"
        field="patentStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.publishNumber')"
        field="publishNumber"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.publishTime')"
        field="publishTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('studentPatent.fields.patentNumber')"
        field="patentNumber"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.patentTime')"
        field="patentTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('studentPatent.fields.personalRanking')"
        field="personalRanking"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.casRanking')"
        field="casRanking"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.imrRanking')"
        field="imrRanking"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.remark')"
        field="remark"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :visible="false"
      />
      <vxe-column
        :title="t('studentPatent.fields.reviewStatus')"
        field="reviewStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('studentPatent.fields.reviewTime')"
        field="reviewTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
        :visible="false"
      />
      <vxe-column
        :title="t('studentPatent.fields.createTime')"
        field="createTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="120"
      >
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row.id)">
            <el-tooltip effect="dark" :content="t('action.detail')" placement="top">
              <el-icon>
                <MessageBox />
              </el-icon>
            </el-tooltip>
          </el-button>
          <!--          <el-button-->
          <!--            v-if="scope.row.reviewStatus === '草稿'"-->
          <!--            link-->
          <!--            type="primary"-->
          <!--            @click="handleSubmitAudit(scope.row.id)"-->
          <!--          >-->
          <!--            <el-tooltip effect="dark" :content="t('action.submitAudit')" placement="top">-->
          <!--              <el-icon><DocumentChecked /></el-icon>-->
          <!--            </el-tooltip>-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            v-if="scope.row.reviewStatus === '已通过'"-->
          <!--            link-->
          <!--            type="primary"-->
          <!--            @click="handleRevoke(scope.row.id)"-->
          <!--          >-->
          <!--            <el-tooltip effect="dark" :content="t('action.revokeEdit')" placement="top">-->
          <!--              <el-icon><RefreshLeft /></el-icon>-->
          <!--            </el-tooltip>-->
          <!--          </el-button>-->
          <el-button
            v-if="scope.row.reviewStatus === '草稿' || scope.row.reviewStatus === '已撤回'"
            link
            type="primary"
            @click="handleDelete(scope.row.id)"
          >
            <el-tooltip effect="dark" :content="t('action.delete')" placement="top">
              <el-icon>
                <Delete />
              </el-icon>
            </el-tooltip>
          </el-button>
          <el-button
            v-if="scope.row.processInstanceId"
            link
            type="primary"
            @click="openProcessInstance(scope.row)"
          >
            <el-tooltip effect="dark" :content="t('action.procInst')" placement="top">
              <el-icon>
                <Tickets />
              </el-icon>
            </el-tooltip>
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StudentPatentForm ref="formRef" @success="getList" />

  <ProcessInstancePopupView ref="processInstancePopupView" />
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import { StudentPatentApi, StudentPatentVO } from '@/api/core/studentpatent'
import StudentPatentForm from './StudentPatentForm.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { Delete, MessageBox, Tickets } from '@element-plus/icons-vue'
import ProcessInstancePopupView from '@/views/bpm/processInstance/detail/ProcessInstancePopupView.vue'
import { StudentPaperVO } from '@/api/core/studentpaper'

/** 专利 列表 */
defineOptions({ name: 'StudentPatent' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<StudentPatentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  accountId: undefined,
  studentCode: undefined,
  studentName: undefined,
  mentorId: undefined,
  mentorCode: undefined,
  mentorName: undefined,
  patentName: undefined,
  publishTime: [],
  patentTime: [],
  patentStatus: undefined,
  publishNumber: undefined,
  patentNumber: undefined,
  personalRanking: undefined,
  casRanking: undefined,
  imrRanking: undefined,
  attachment: undefined,
  remark: undefined,
  reviewStatus: undefined,
  reviewTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentPatentApi.getStudentPatentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof StudentPatentForm>>()
const openForm = (id?: string) => {
  formRef.value?.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StudentPatentApi.deleteStudentPatent(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleSubmitAudit = async (id: string) => {
  try {
    await message.confirm(t('studentPatent.audit.submitTip'))
    await StudentPatentApi.submitStudentPatent(id)
    message.success(t('common.applySuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleRevoke = async (id: string) => {
  try {
    await message.confirm(t('studentPatent.audit.revokeTip'))
    await StudentPatentApi.revokeStudentPatent(id)
    message.success(t('common.operateSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const processInstancePopupView = ref<InstanceType<typeof ProcessInstancePopupView>>()
const openProcessInstance = (row: StudentPaperVO) => {
  processInstancePopupView.value?.show(row.processInstanceId)
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
